from flask_login import current_user
from flask_restful import Resource, marshal_with, reqparse  # type: ignore
from controllers.errors.error import HttpAccountNotFound
from controllers.errors.roles_errors import HTTPSysMenuNotFoundError, HTTPSysRoleNotFoundError, HTTPDelBuildInRoleError
from core.roles.role_check import check_perms
from libs.login import login_required
from controllers.console import api
from services.account_service import AccountService
from services.errors.account import AccountNotFoundError
from services.errors.roles import SysMenuNotFoundError, SysRoleNotFoundError, DelBuildInRoleError
from werkzeug.exceptions import InternalServerError
from fields.account_fields import account_with_role_list_fields
from services.role_service import RoleService

class UserSearchApi(Resource):
    @login_required
    @check_perms(perms=["user:search"])
    @marshal_with(account_with_role_list_fields)
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument("param",type=str,required=False,location='args')
        args = parser.parse_args()
        param = args.get("param")
        
        return AccountService.search_accounts(current_user.current_tenant_id, param)
    
api.add_resource(UserSearchApi,'/users/search')